package com.xzt.meeting.dao;

import com.xzt.meeting.entity.User;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author xzt
 * @Date 2022/5/5 22:50
 * @Version 1.0
 * @Introduce
 */
public class UserDao extends BaseDao{
    public User findUserByname(String username) {
        try{
            return jdbcTemplate.queryForObject("select * from user where username=?",
                    new BeanPropertyRowMapper<>(User.class),username);
        }catch (EmptyResultDataAccessException throwables){
            return null;
        }
    }

    public User findUserByeamil(String email) {
        try{
            return jdbcTemplate.queryForObject("select * from user where email=?",
                    new BeanPropertyRowMapper<>(User.class),email);
        }catch (EmptyResultDataAccessException throwables){
            return null;
        }
    }

    public void save(User user) {
        jdbcTemplate.update("insert into user values (null,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
                user.getUsername(),
                user.getPassword(),
                user.getEmail(),
                user.getQqOpenid(),
                user.getWxOpenid(),
                user.getRealName(),
                user.getAge(),
                user.getPhone(),
                user.getGender(),
                user.getInfo(),
                user.getRegisterTime(),
                user.getLoginTime(),
                user.getPic(),
                user.getLook(),
                user.getIsSecret(),
                user.getDeptName(),
                user.getDeptId());
    }

    public List<User> findAll() {
        return jdbcTemplate.query("select * from user",
                new BeanPropertyRowMapper<>(User.class));
    }

    public List<User> search(String realName) {
        try{
            return jdbcTemplate.query("select * from user where realName like ?",
                    new BeanPropertyRowMapper<>(User.class),"%" + realName + "%");
        }catch (EmptyResultDataAccessException throwables){
            throwables.printStackTrace();
        }
        return null;
    }

    public Integer countByRealName(String realName) {
        try {
            return jdbcTemplate.queryForObject(
                    "select count(1) from user where real_name like ?",
                    Integer.class, "%" + realName + "%");
        } catch (EmptyResultDataAccessException exception) {
            return null;
        }
    }

    public List<User> findPage(String realName, Integer startIndex, Integer pageSize) {
        List<User> userList = jdbcTemplate.query(
                "select * from user where real_name like ? order by login_time desc limit ?,?  ",
                new BeanPropertyRowMapper<>(User.class),
                "%" + realName + "%", startIndex, pageSize);

        return userList;
    }

    public List<Map<String, Object>> findUserByDeptId(Long deptId) {
        return jdbcTemplate.queryForList(
                "select id,real_name realName,is_secret isSecret from user where dept_id=?",
                deptId);
    }

    public User findUserById(Long id) {
        try{
            return jdbcTemplate.queryForObject("select * from user where id=?",
                    new BeanPropertyRowMapper<>(User.class),id);
        }catch (EmptyResultDataAccessException throwables){
            throwables.printStackTrace();
        }
        return null;
    }

    public void incLook(Long id) {
        jdbcTemplate.update("update user set look=look+1 where id=?", id);
    }

    public Integer countFansByUserId(Long id) {
        try {
            return jdbcTemplate.queryForObject(
                    "select count(*) from userfocus where user_focus_id=?",
                    Integer.class, id);
        } catch (EmptyResultDataAccessException exception) {
            // 如果是没有查询到则返回null
            return null;
        }
    }

    public Integer countFocusByUserId(Long id) {
        try {
            return jdbcTemplate.queryForObject("select count(*) from userfocus where user_id=?",
                    Integer.class, id);
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }

    public void updateUrl(Long id, String picUrl) {
        jdbcTemplate.update("update user set pic=? where id=?", picUrl, id);
    }

    public void update(User user) {
        jdbcTemplate.update(
                "update user set password=?,real_name=?,age=?,phone=?,gender=?,info=?,is_secret=?,dept_name=?,dept_id=? where id=?",
                user.getPassword(),
                user.getRealName(),
                user.getAge(),
                user.getPhone(),
                user.getGender(),
                user.getInfo(),
                user.getIsSecret(),
                user.getDeptName(),
                user.getDeptId(),
                user.getId());
    }

    public List<Long> findFocusListByUserId(Long id) {
        return jdbcTemplate.query("select user_focus_id from userfocus uf where uf.user_id=?", new RowMapper<Long>() {
            @Override
            public Long mapRow(ResultSet resultSet, int i) throws SQLException {
                return resultSet.getLong("user_focus_id");
            }
        }, id);
    }

    public void focus(Long userid, Long focusid) {
        jdbcTemplate.update("insert into userfocus values(?,?)", userid, focusid);
    }

    public void unFocus(Long userid, Long focusid) {
        jdbcTemplate.update("delete from userfocus where user_id=? and user_focus_id=?", userid, focusid);
    }

    public List<Map<String, Object>> findFocusPage(Long id, Integer startIndex, Integer pageSize) {
        String sql="select u.id,u.real_name realName,u.is_secret isSecret from user u where u.id in \n" +
                "    (\n" +
                "        select user_focus_id from userfocus uf where user_id=?\n" +
                "    )\n" +
                "    limit ?,?";
        return jdbcTemplate.queryForList(sql,id,startIndex,pageSize);
    }

    public void updateLoginTime(Long uid) {
        jdbcTemplate.update("update user set login_time=now() where id=?",uid);
    }

    public void updatePwdByEmail(String email, String newpwd) {
        jdbcTemplate.update("update user set password=? where email=?",newpwd,email);
    }

    public User findByWxOpenId(String openid) {

        try {
            return jdbcTemplate.queryForObject("select * from user where wx_openid=?",
                    new BeanPropertyRowMapper<>(User.class),openid);
        }catch (EmptyResultDataAccessException e){
            return null;
        }
    }
}
